<?php
/**
 * News Feeds
 * Every events,tasks that are relative to the contact
 *
 * @package contact
 */
use Fox\Services\Wsdl\Generate;
use Fox\Behaviors;
use Fox\Html\Button;
use Hive\Ressource;
use Fox\Generator;
use Hive\Mission;
use Hive\Ressource\Wish;
use Hive\Ressource\Experience;
use Fox\Html\Stream\News;
use Fox\Html\Stream;

/* @var $contact Hive\Ressource */
$xview = Xview::instance();
$contact = $xview->component('ressource', 'object');
// Customizing
$customizing = Hive\Ressource\Customizing::instance();
$customizing->trackSummaryPage(array('mod' => 'ressource','act'=>'newsfeed'));

// Page Title
$header = new Fox\Html\Header\Content('page-header',Msg(Hive::BUNDLE,Hive::MENU_NEW_FEEDS));

$rid = $contact->getId();
$data = $contact->getData();

$stream = new Stream('stream');
foreach($contact->news() as $timestamp => $item)
{
	$object = $item['object'];
	$titleClass = null;
	switch ($item['type'])
	{
		case Experience::className():
			/* @var $object Hive\Ressource\Experience */
			$startDate = new DateTime($object->start_date);
			$monthLabel = Fox\Ddic\DataElement\Text::label('month',$startDate->format('m'));
			$endDate = $object->end_date;
			if(empty($endDate))
			{
				$title = Msg(Hive::BUNDLE,Hive::MSG_JOB_CURRENT,$monthLabel,$startDate->format('Y'),
								Msg(Hive::BUNDLE,Hive::MSG_POSITION,$object->jobtitle,$object->companyName())
					);
			}
			else
			{
				$endDate = new DateTime($object->end_date);
				$monthEndLabel = Fox\Ddic\DataElement\Text::label('month',$endDate->format('m'));
				$title = Msg(Hive::BUNDLE,Hive::MSG_JOB_CURRENT,
								$monthLabel.' '.$startDate->format('Y'),
								$monthEndLabel.' '.$endDate->format('Y'),
								Msg(Hive::BUNDLE,Hive::MSG_POSITION,$object->jobtitle,$object->companyName())
					);
			}
			$content = $object->comment;
			$titleClass = 'job';
		break;
		case Wish::className():
			$wisheDate = new DateTime($object->cdate);
			$date = Msg(Hive::BUNDLE,Hive::MSG_NEW_DATE,
						$wisheDate->format('d'),
						Fox\Ddic\DataElement\Text::label('month',$wisheDate->format('m')),
						$wisheDate->format('Y'));

			/* @var $object Hive\Ressource\Wish */
			$title = Msg(Hive::BUNDLE,Hive::MSG_WHISHE_TITLE,$date,$object->joblist,$object->fullSalary());
			$content = $object->comment;
			$titleClass = 'wishe';
		break;
		case Mission::className():
			$addedDate = new DateTime($timestamp);
			$date = Msg(Hive::BUNDLE,Hive::MSG_NEW_DATE,
						$addedDate->format('d'),
						Fox\Ddic\DataElement\Text::label('month',$addedDate->format('m')),
						$addedDate->format('Y'));
			$title = Msg(Hive::BUNDLE,Hive::MSG_MISSION_ADDED,$date,$object->label(),$object->companyName());
			$content = $object->description;
			$titleClass = 'mission';
		break;
	}
	$stream->add(new News($item['type'].$object->getId(),$title,$content))
				->titleClass($titleClass);
}

$actions = null;
// Handle Last Used Mission Event
if(isset($_SESSION[Mission::LAST_USED]) && !empty($_SESSION[Mission::LAST_USED])) {
	$mid = $_SESSION[Mission::LAST_USED];
	// Check first if user is not already in the mission
	$mission = $contact->findMission($mid);
	if($mission) {
		$change = new Button('change','?',Msg(Hive::BUNDLE,Hive::BUT_REMOVE_FROM_MISSION,$mission->label()));
		$change->color(Button::COLOR_RED)
				->behaviors(Behaviors::action(Generator::url('missions','ressource','unassign',
					array('missionid' => $mid,'partnerid' => $rid)),false,null,null,false,'POST',null,'change','change',true));
		$header->add($change);
	} else {
		try {
			$mission = Fox\Dao::load(Mission::className(),$mid);
			$change = new Button('change','?',Msg(Hive::BUNDLE,Hive::BUT_ASSIGN_TO_MISSION,$mission->label()));
			$change->behaviors(Behaviors::action(Generator::url('missions','ressource','assign',
					array('missionid' => $mid,'data' => '['.$rid.']')),false,null,null,false,'POST',null,'change','change',true));
			
			$header->add($change);
		} catch (Fox\Object\Exception\NotExist $e) {
			
		}
	}
}

$html = $stream->render();
if(count($contact->news()) == 0) {
	$html .= '<p class="prettyprint">'.Msg(Hive::BUNDLE,Hive::NEWS_FEED_IS_EMPTY).'</p>';
}
?>
<div>
	<div class="page-header clearfix">
		<?php echo $header->render();?>
	</div>
	<div class="contentArea">
		<div class="mbs">
			<?php echo $html;?>
		</div>
	</div>
</div>